/*
 * @Author: panzhiwei 749458928@qq.com
 * @Date: 2024-01-11 18:29:53
 * @LastEditors: panzhiwei 749458928@qq.com
 * @LastEditTime: 2024-02-01 17:40:06
 * @FilePath: /NewProject/assets/Script/PlayerController.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { _decorator, Component, Vec3, view, input, Input, instantiate, Prefab, Node, Vec2, Camera, math } from 'cc';
import { EnemySpawner } from './EnemySpawner'; // 导入 EnemySpawner 类
import { Arrow } from './weapon/Arrow'; // 确保路径正确
import { ArrowSpawner } from './weapon/ArrowSpawner'; // 确保路径正确
const { ccclass, property } = _decorator;

export const BLOCK_SIZE = 40; // 添加一个放大比

@ccclass('PlayerController')
export class PlayerController extends Component {

    // 弓的预制体
    @property({ type: Prefab })
    public bowPrefab: Prefab | null = null;

    // // 箭的预制体
    // @property({ type: Prefab })
    // public arrowPrefab: Prefab | null = null;
    // // 毒箭的预制体
    // @property({ type: Prefab })
    // public PoisonArrowPrefab: Prefab | null = null;
    // // 火箭弩的预制体
    // @property({ type: Prefab })
    // public RoketCrossbowPrefab: Prefab | null = null;

    // 弓箭生成器
    @property({ type: ArrowSpawner })
    public arrowSpawner: ArrowSpawner | null = null;

    // 敌人生成器
    @property({ type: EnemySpawner })
    public enemySpawner: EnemySpawner | null = null;

    @property
    public shootingSpeed: number = 50;// 射击速度


    start() {
        // input.on(Input.EventType.MOUSE_UP, this.onShootEvent, this);
        // this.schedule(this.onShootEvent, 2);
        this.schedule(this.onRoketCrossbowShootEvent, 2);
    }

    onLoad() {

        // 获取视图大小
        // const winSize = view.getVisibleSize();

        const bowPosition = new Vec3(0, -450, 0); // 底部中央
        // const arrowOffsetY = 20; // 箭相对于弓的偏移量
        // const arrowPosition = new Vec3(0, -450, 0); // 在弓的上方


        // 创建弓和箭的实例
        const bow = instantiate(this.bowPrefab);
        // const arrow = instantiate(this.arrowPrefab);
        // const poisonArrow = instantiate(this.PoisonArrowPrefab);
        // const roketCrossbow = instantiate(this.RoketCrossbowPrefab);

        // 设置位置
        bow.setPosition(bowPosition);
        // arrow.setPosition(arrowPosition);
        // poisonArrow.setPosition(arrowPosition);
        // roketCrossbow.setPosition(arrowPosition);

        // 将弓和箭添加到场景中
        this.node.addChild(bow);
        // this.node.addChild(arrow);
    }

    update(deltaTime: number) {

    }

    // 射击方法
    shoot() {
        const arrowPosition = new Vec3(0, -450, 0); // 箭的起始位置
        // 寻找最近的敌人
        console.log("before-nearestEnemy")
        const nearestEnemy = this.findNearestEnemy(arrowPosition);
        console.log("after-nearestEnemy")
        if (!nearestEnemy) {
            console.warn("No enemies found");
            return;
        }

        // 计算射击方向
        let direction = Vec3.subtract(new Vec3(), nearestEnemy.position, arrowPosition);
        Vec3.normalize(direction, direction);

        // 执行射击
        // let arrow = arrowNode.getComponent(Arrow);
        if (this.arrowSpawner) {
            this.arrowSpawner.shoot(direction,2,10,50,arrowPosition);
        }
    }

    // 射击方法
    roketCrossbowShoot() {
        const arrowPosition = new Vec3(0, -450, 0); // 箭的起始位置
        // 寻找最近的敌人
        console.log("before-nearestEnemy")
        const nearestEnemy = this.findNearestEnemy(arrowPosition);
        console.log("after-nearestEnemy")
        if (!nearestEnemy) {
            console.warn("No enemies found");
            return;
        }

        // 计算射击方向
        let direction = Vec3.subtract(new Vec3(), nearestEnemy.position, arrowPosition);
        Vec3.normalize(direction, direction);

        // 执行射击
        // let arrow = arrowNode.getComponent(Arrow);
        if (this.arrowSpawner) {
            this.arrowSpawner.roketCrossbowShoot(direction,2,10,50,arrowPosition);
        }
    }

    findNearestEnemy(arrowPosition: Vec3): Node | null {
        if (!this.enemySpawner) {
            return null;
        }
    
        let lowestEnemies: Node[] = [];
        let lowestY = Infinity;
        this.enemySpawner.node.children.forEach(enemy => {
            if (enemy.position.y < lowestY) {
                lowestY = enemy.position.y;
                lowestEnemies = [enemy];
            } else if (enemy.position.y === lowestY) {
                lowestEnemies.push(enemy);
            }
        });
    
        if (lowestEnemies.length === 0) {
            return null;
        }
    
        let nearestEnemy: Node | null = null;
        let nearestDistance = Infinity;
        lowestEnemies.forEach(enemy => {
            let distance = Vec3.distance(arrowPosition, enemy.position);
            if (distance < nearestDistance) {
                nearestDistance = distance;
                nearestEnemy = enemy;
            }
        });
    
        return nearestEnemy;
    }
    

    // 在某个事件（如按钮点击或屏幕触摸）中调用射击方法
    onShootEvent() {
        this.shoot();
    }

    onRoketCrossbowShootEvent() {
        this.roketCrossbowShoot();
    }

}

